package com.hdcircles.autoform.presenter;

import com.hdcircles.autoform.base.RxPresenter;
import com.hdcircles.autoform.model.bean.WelcomeBean;
import com.hdcircles.autoform.model.http.RetrofitHelper;
import com.hdcircles.autoform.presenter.contract.WelcomeContract;
import com.hdcircles.autoform.util.RxUtil;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import rx.Observable;
import rx.Subscription;
import rx.functions.Action1;

/**
 * Created by hdcircles on 2016/9/23.
 */

public class WelcomePresenter extends RxPresenter<WelcomeContract.View> implements WelcomeContract.Presenter {

    private static final String RES="1080*1776";

    private static final int COUNT_DOWN_TIME=2200;

    private RetrofitHelper retrofitHelper;

    @Inject
    public WelcomePresenter(RetrofitHelper retrofitHelper){
        this.retrofitHelper=retrofitHelper;
    }

    @Override
    public void getWelcomeData() {
        Subscription subscription=retrofitHelper.fetchWelcomeInfo(RES)
                .compose(RxUtil.<WelcomeBean>rxScheduleHelper())
                .subscribe(new Action1<WelcomeBean>() {
                    @Override
                    public void call(WelcomeBean welcomeBean) {
                        mView.showContent(welcomeBean);
                        startCountDown();
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        mView.showError("");
                        mView.jump2Main();
                    }
                });
        addSubscribe(subscription);

    }

    private void startCountDown(){
        Subscription subscription= Observable.timer(COUNT_DOWN_TIME, TimeUnit.MILLISECONDS)
                .compose(RxUtil.<Long>rxScheduleHelper())
                .subscribe(new Action1<Long>() {
                    @Override
                    public void call(Long aLong) {
                        mView.jump2Main();
                    }
                });
        addSubscribe(subscription);
    }
}
